package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Collection;
import com.tour.mapper.CollectionMapper;
import com.tour.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2020-12-30
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Autowired
    CollectionMapper collectionMapper;

    public ResponseWrapper addCollection(Collection collection){
        if (collection != null){
            int result = collectionMapper.insert(collection);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"收藏景区成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper isCollection(Integer userId ,Integer scenicId){
        if (userId == null || scenicId == null){
            return ResponseWrapper.markError();
        }else {
            QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
            collectionQueryWrapper.eq("user_id",userId).eq("scenic_id",scenicId);
            int result = collectionMapper.selectCount(collectionQueryWrapper);
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(result);
            if (result == 1){
                wrapper.getData().put("isCollection",true);
            }else {
                wrapper.getData().put("isCollection",false);
            }
            return wrapper;

        }
    }

    public ResponseWrapper delCollection(Integer userId ,Integer scenicId){
        if (userId == null || scenicId == null){
           return ResponseWrapper.markError();
        }else {
            QueryWrapper<Collection> collectionQueryWrapper = new QueryWrapper<>();
            collectionQueryWrapper.eq("user_id",userId).eq("scenic_id",scenicId);
            int result = collectionMapper.delete(collectionQueryWrapper);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"取消收藏景区成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper findAllCollection(Integer userId,Integer pageNum,Integer pageSize){
        if (userId != null){
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> iPage = collectionMapper.findAllCollection(page,userId);
            List<Map> list = iPage.getRecords();
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",iPage.getTotal());
            wrapper.getData().put("pageSize",iPage.getSize());
            wrapper.getData().put("pageNum",iPage.getCurrent());
            return wrapper;
        }else {
            return ResponseWrapper.markError();
        }
    }
}
